/**
 *
 * @param {function} func  执行函数
 * @param {number} wait  延迟时间
 * @returns
 */

export function debounce(func, wait) {
  var timer;
  return function () {
    var _this = this; // 记录当前this
    var arg = arguments; // 记录参数
    clearTimeout(timer);
    timer = setTimeout(function () {
      func.apply(_this, arg); //将 func的this改为_this
    }, wait);
  };
}

/**
 *
 * @param {number} timestamp 时间戳
 * @returns
 */
export function timestampToTime(timestamp = new Date().getTime()) {
  var date = new Date(timestamp);
  var Y = date.getFullYear() + "-";
  var M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return Y + M + D;
}

/**
 *
 * @returns 返回一随机的uuid
 */
export function uuid() {
  return (function () {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
      d += performance.now(); //use high-precision timer if available
    }
    var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
      /[xy]/g,
      function (c) {
        var r = (d + Math.random() * 16) % 16 | 0; // d是随机种子
        d = Math.floor(d / 16);
        return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
      }
    );
    return uuid;
  })();
}
